package com.otod.pm.doc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.doc.mapper.PmDocTpItemMapper;
import com.otod.pm.doc.vo.PmDocTpItemVo;

@Service
public class DocTpService extends BaseService{
	protected static final Logger logger = LoggerFactory.getLogger(DocTpService.class);
	
	@Autowired  
	private PmDocTpItemMapper docTpMapper;
	
	
	
	
	
	
	
	/**
	 * 查询菜单列表
	 */
	public List<PmDocTpItemVo> queryForList(Map<String, Object> params){
		return docTpMapper.queryForList(params);
	}
	
	/**
	 * 查询Item
	 * param id
	 */
	public PmDocTpItemVo getById(String id){
		return docTpMapper.getById(id);
	}
	
	
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@Transactional
	public void deleteById(String itemId) throws AppException{
		//List<AuapTMenuVo> childrenList = queryChildrenForList(itemId);
		/*if(childrenList.size()>0){
			throw new AppException("只能删除无子节点的菜单！");
		}
		*/
		docTpMapper.deleteById(itemId);
	}
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@Transactional
	public void deleteByTpId(String tpId) throws AppException{
		
		docTpMapper.deleteByTpId(tpId);
	}
	
	
	
	
	
	/**
	 * 新增
	 */
	@Transactional
	public void insert(PmDocTpItemVo docTpItemVo){
		docTpItemVo.setUuid(super.getUUID());
		docTpMapper.insert(docTpItemVo);
	}
	
	/**
	 * 修改
	 */
	@Transactional
	public void update(Map map){
		docTpMapper.update(map);
	}
	
	
	
	/**
	 * 获取当前item的子孙节点
	 * @param pMenuId 父级菜单ID
	 * @return
	 */
	public List<PmDocTpItemVo> queryChildrenForList(String pMenuId,String tpId){
		List<PmDocTpItemVo> childrenList = new ArrayList<PmDocTpItemVo>();
		if(StringUtils.isNotEmpty(tpId)){
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("tpId", tpId);
			List<PmDocTpItemVo> allList = docTpMapper.queryForList(params);
			if(pMenuId != null){
				
				getChildrenMenu(pMenuId, childrenList, allList);
				return childrenList;
			}
			return allList;
		}
		return childrenList;
	}
	
	/**
	 * 子节点菜单递归
	 * @param pMenuId
	 * @param childrenList
	 * @param allList
	 */
	private void getChildrenMenu(String pMenuId,List<PmDocTpItemVo> childrenList,List<PmDocTpItemVo> allList){
		for (PmDocTpItemVo itemVo : allList) {
			if(pMenuId.equals(itemVo.getParentId())){
				childrenList.add(itemVo);
				getChildrenMenu(itemVo.getUuid(), childrenList, allList);
			}
		}
	}
	/**
	 * 判断章节名字是否重复
	 * @param pmDocTpItemVo
	 * @throws AppException 
	 */
	public void selectItemTitle(PmDocTpItemVo pmDocTpItemVo) throws AppException{
		Map<String, Object> map = new HashMap<String, Object>();
	
		map.put("tpId", pmDocTpItemVo.getTpId());
		map.put("title", pmDocTpItemVo.getTitle());
		map.put("parentId", pmDocTpItemVo.getParentId());
		List<PmDocTpItemVo> docTpItemVos = docTpMapper.queryForList(map);
		if(!docTpItemVos.isEmpty()){
			throw new AppException("章节名字重复");
		}
		
	}
}
